package channel;

import core.AnalogSignal;
import modulation.ModulatorSignal;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Eugene Khoroshutin
 * Date: 1/6/13
 * Time: 12:26 AM
 */
public class Propagator {

    public List<ModulatorSignal> modulatedSequence;
    public List<PropagatorSignal> delayedSequence = new ArrayList<PropagatorSignal>();
    public Double amplitudeMultiplier;
    public Double timeShift;


    public Propagator(List<ModulatorSignal> modulatedSequence, Double amplitudeMultiplier, Double timeShift) {
        this.modulatedSequence = modulatedSequence;
        this.amplitudeMultiplier = amplitudeMultiplier;
        this.timeShift = timeShift;
        int modulatedSequenceSize = modulatedSequence.size();
        int mSequenceImpulsePosition = 0;

        //determine left (zeros) part of the sequence
        for (int i = 0; i < modulatedSequenceSize; i++) {
            if (timeShift > modulatedSequence.get(i).getEnd()) {
                mSequenceImpulsePosition += 1;
            } else if (timeShift == modulatedSequence.get(i).getEnd()) {
                mSequenceImpulsePosition = i + 1;//end of old equals start of new pulse
            }
        }

        //initialize left part with zeros
        for (int i = 0; i < mSequenceImpulsePosition; i++) {
            delayedSequence.add(new PropagatorSignal(modulatedSequence.get(i).getFrequency(), 0, 0, modulatedSequence.get(i).getStart(), modulatedSequence.get(i).getEnd()));
        }
        double start = modulatedSequence.get(mSequenceImpulsePosition).getStart();
        double end = modulatedSequence.get(mSequenceImpulsePosition).getEnd();
        double impulseLength = end - start;
        double xLimit = modulatedSequence.get(modulatedSequenceSize - 1).getEnd();

        if (timeShift > start) {
            delayedSequence.add(new PropagatorSignal(modulatedSequence.get(mSequenceImpulsePosition).getFrequency(), 0, 0, start, timeShift));
        }
        int modulatedSequenceCounter = 0;
        for (double i = timeShift; xLimit - i >= impulseLength; i = i + impulseLength) {
            delayedSequence.add(new PropagatorSignal(modulatedSequence.get(modulatedSequenceCounter).getFrequency(), amplitudeMultiplier * modulatedSequence.get(modulatedSequenceCounter).getAmplitude(), 0, i, i + impulseLength));
            modulatedSequenceCounter += 1;
            mSequenceImpulsePosition += 1;
        }
        if (delayedSequence.get(mSequenceImpulsePosition - 1).getEnd() != xLimit) {
            delayedSequence.add(new PropagatorSignal(modulatedSequence.get(modulatedSequenceCounter).getFrequency(), amplitudeMultiplier * modulatedSequence.get(modulatedSequenceCounter).getAmplitude(), 0, delayedSequence.get(mSequenceImpulsePosition-1).getEnd(), xLimit));
        }
    }
}


